﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using System.Data;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.VehicleReport.BLL;

public partial class DutyLog_List : System.Web.UI.Page
{
    private string DepartmentID;
    string strBaseDate = "2009-8-24";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Bind();
            //bindChart();
            lbtnQuery_Click(this, new EventArgs());

        }

    }

    private void Bind()
    {
        DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        dptDepartment1.DataSource = Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000' and DepartmentID<>'1300000'");
        dptDepartment1.DataTextField = "FullName";
        dptDepartment1.DataValueField = "DepartmentID";
        dptDepartment1.DataBind();
        this.dptDepartment1.SelectedValue = DepartmentID.Substring(0, 4) + "000";
        dptDepartment1_SelectedIndexChanged(this, new EventArgs());

        if (!DepartmentID.Substring(2, 3).Equals("000"))
        {
            if (DepartmentID.Substring(4, 1).Equals("0"))
            {
                this.dptDepartment1.Enabled = false;
            }
            else
            {
                if (dptDepartment2.Items.FindByValue(DepartmentID) != null)
                {
                    dptDepartment2.SelectedValue = DepartmentID;
                    this.dptDepartment1.Enabled = false;
                    this.dptDepartment2.Enabled = false;
                }
                else
                {

                    this.dptDepartment1.Enabled = false;
                    if (DepartmentID.Substring(4, 1) == "2")
                    {
                        string ShortName = Yipusoft.Account.BLL.Department.GetDepartment(DepartmentID).ShortName.Substring(0, 2);
                        List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(" departmentid like '" + DepartmentID.Substring(0, 4) + "3__' and ShortName like '" + ShortName + "%'  ").ToList();
                        if (departments.Count > 0)
                        {
                            dptDepartment2.Items.Clear();
                            dptDepartment2.DataSource = departments;
                            dptDepartment2.DataBind();
                        }
                        else
                        {
                            dptDepartment2.SelectedValue = DepartmentID;
                            this.dptDepartment1.Enabled = false;
                            this.dptDepartment2.Enabled = false;
                        }

                    }
                }

            }
        }


        for (int i = 2009; i < 2015; i++)
        {
            dptYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            dptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        dptMonth.SelectedValue = DateTime.Now.Month.ToString();
        dptYear.SelectedValue = DateTime.Now.Year.ToString();
        if (Request["Departmentid"] != null)
        {
            dptDepartment1.SelectedValue = Request["Departmentid"].Substring(0, 4) + "000";
            BindDepartment2();
            dptDepartment2.SelectedValue = Request["Departmentid"];
            dptYear.SelectedValue = Request["y"];
            dptMonth.SelectedValue = Request["m"];
        }



    }
    protected void calSchedule_DayRender(object sender, DayRenderEventArgs e)
    {
        DepartmentID = dptDepartment2.SelectedValue;
        CalendarDay d = ((DayRenderEventArgs)e).Day;
        TableCell c = ((DayRenderEventArgs)e).Cell;

        DateTime BaseDate = Convert.ToDateTime(strBaseDate);
        if (d.IsOtherMonth)
        {
            c.Controls.Clear();
        }
        else
        {
            try
            {
                int cuDay = Convert.ToInt32(d.Date.DayOfWeek);
                int temp = 0;
                if (cuDay != 0)
                {
                    temp = ((-1) * cuDay) + 1;
                }
                else
                {
                    temp = -6;
                }
                DateTime StartTime = DateTime.Parse(d.Date.AddDays(temp).ToString("yyyy-MM-dd"));
                DateTime EndTime = DateTime.Parse(StartTime.AddDays(6).ToString("yyyy-MM-dd"));



                c.Controls.Add(new LiteralControl("<div style=\"text-align:center; width:100%;height:20px; line-height:22px;font-weight:normal;\">"));
                DiarySquadron diary = Diary.GetDiaryByDepartmentIDAndDataTime(DepartmentID, d.Date);
                if (diary != null)
                {

                    c.Controls.Add(new LiteralControl("<img src='../../Images/add_small.gif' alt=''/><a href='DiaryDisplay.aspx?id=" + diary.Id + "' title='要事记录'>要事记录</a>"));
                    c.Controls.Add(new LiteralControl("<br/>"));
                }

                if (Duty.GetDutyLog(d.Date, DepartmentID.Trim()) != null)
                {
                    if (d.Date >= BaseDate)
                    {
                        c.Controls.Add(new LiteralControl("<img src='../../Images/add_small.gif' alt=''/><a href='DutyView.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title='执勤记录'>执勤记录</a>"));
                        c.Controls.Add(new LiteralControl("<br/>"));
                    }
                    else
                    {
                        c.Controls.Add(new LiteralControl("<img src='../../Images/add_small.gif' alt=''/><a href='DutyView1.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title='执勤记录'>执勤记录</a>"));
                        c.Controls.Add(new LiteralControl("<br/>"));
                    }
                }

                //车辆检查
                if (VehicleDayItem.GetList(" DepartmentId='" + DepartmentID + "' and DayDate='" + d.Date + "' ").Count > 0)
                {
                    c.Controls.Add(new LiteralControl("<img src='../../images/add_small.gif' alt=''/><a href='VehicleReportView.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title='车辆检查'>车辆检查</a>"));
                    c.Controls.Add(new LiteralControl("<br/>"));

                }

                //车场登记
                //CarOrder park = Park.GetParkLog2(StartTime, EndTime, DepartmentID);
                ParkLogs park = Park.GetParkLog(StartTime, EndTime, DepartmentID);
                if (park != null && park.Date.Value.ToShortDateString() == d.Date.ToShortDateString())
                {
                    if (d.Date > Convert.ToDateTime("2009-9-27"))
                    {
                        c.Controls.Add(new LiteralControl("<img src='../../Images/add_small.gif' alt=''/><a href='ParkLogView1.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title=''>车场登记</a>"));
                    }
                    else
                    {
                        c.Controls.Add(new LiteralControl("<img src='../../Images/add_small.gif' alt=''/><a href='ParkLogView.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title=''>车场登记</a>"));
                    }
                    c.Controls.Add(new LiteralControl("<br/>"));
                    c.Controls.Add(new LiteralControl("</div>"));
                }
            }
            catch (Exception exc)
            {
                Response.Write(exc.ToString());

            }


        }
    }

    protected void dptDepartment1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartment2();
    }

    private void BindDepartment2()
    {
        dptDepartment2.DataSource = Yipusoft.Account.BLL.Department.GetList("Left(DepartmentID,4)='" + dptDepartment1.SelectedValue.Substring(0, 4) + "' and SubString(DepartmentID,5,1)='3'");
        dptDepartment2.DataTextField = "FullName";
        dptDepartment2.DataValueField = "DepartmentID";
        dptDepartment2.DataBind();
    }
    protected void lbtnQuery_Click(object sender, EventArgs e)
    {
        this.calSchedule.SelectedDate = new DateTime(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth
            .SelectedValue), 1);
        this.calSchedule.VisibleDate = new DateTime(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), 1);
        bindChart();
        this.UserChart.Titles[0].Text = dptDepartment2.SelectedItem.Text + dptYear.SelectedItem.Text + dptMonth.SelectedItem.Text + "未完成率统计图";

        int year = Convert.ToInt32(dptYear.SelectedValue);
        int month = Convert.ToInt32(dptMonth.SelectedValue);
        string ChangeWhere = " departmentid='" + dptDepartment2.SelectedValue + "' and year(DateChange)=" + year + " and month(DateChange)=" + month + " ";

        lblSodierChange.Text = Diary.DiaryChangesGetList(ChangeWhere + " and flag=0 ").Count.ToString();
        lblSodierChange.NavigateUrl = "ChangeView.aspx?where=" + ChangeWhere + " and flag=0&Flag=0";
        lblDeviceChange.Text = Diary.DiaryChangesGetList(ChangeWhere + " and flag=1 ").Count.ToString();
        lblDeviceChange.NavigateUrl = "ChangeView.aspx?where=" + ChangeWhere + " and flag=1&Flag=1";
        string leaveWhere = " DepartmentId='" + dptDepartment2.SelectedValue + "'and year(DiaryDate)=" + year + " and month(DiaryDate)=" + month + " ";
        lblLeave.Text = Diary.DiaryLeaveGetList(leaveWhere).Count.ToString();
        lblLeave.NavigateUrl = "LeaveView.aspx?where=" + leaveWhere;

    }
    DiaryDataContext diarydb = new DiaryDataContext();
    DutyLogDataContext dutydb = new DutyLogDataContext();
    ParkLogDataContext parkdb = new ParkLogDataContext();
    void bindChart()
    {
        int year = Convert.ToInt32(dptYear.SelectedValue);
        int month = Convert.ToInt32(dptMonth.SelectedValue);
        int monthDayCount = Account.GetMonthDayCount(year, month);
        int monthFridayCount = Account.GetMonthFridayCount(year, month);
        //总数
        int diaryTotal = monthDayCount;
        int parkTotal = monthFridayCount;
        int DutyTotal = monthDayCount;
        DepartmentID = dptDepartment2.SelectedValue;
        DepartmentInfo info = Department.GetDepartment(DepartmentID);

        //要事日记
        //int diarysum = diarydb.DiarySquadron.Where(o => ).GroupBy(o=>o.DepartmentId,o => o.DiaryDate).Count();

        int diarysum = (from o in diarydb.DiarySquadron where o.DepartmentId == info.DepartmentID && o.DiaryDate.Value.Year == year && o.DiaryDate.Value.Month == month group o by new { o.DepartmentId, o.DiaryDate } into k select k.Key).Count();

        if (diaryTotal == 0)
        {

            UserChart.Series["查看项目"].Points.AddXY("要事记录", DBNull.Value);
        }
        else
        {
            decimal agv = (decimal)(diaryTotal - diarysum) / (diaryTotal);
            UserChart.Series["查看项目"].Points.AddXY("要事记录", agv);

        }

        // 执勤日录

        // int dutysum = dutydb.DutyLogs.Where(o => o.SetDepartment == info.DepartmentID && o.StartTime.Value.Year == year && o.StartTime.Value.Month == month).Count();
        //int dutysum = dutydb.DutyLogs.Where(o => o.SetDepartment == info.DepartmentID && o.StartTime.Value.Year == year && o.StartTime.Value.Month == month).Distinct().Count();
        int dutysum = (from o in dutydb.DutyLogs where o.SetDepartment == info.DepartmentID && o.StartTime.Value.Year == year && o.StartTime.Value.Month == month group o by new { o.SetDepartment, o.StartTime.Value.Year, o.StartTime.Value.Month, o.StartTime.Value.Day } into k select k.Key).Count();

        if (DutyTotal == 0)
        {

            UserChart.Series["查看项目"].Points.AddXY("执勤记录", DBNull.Value);
        }
        else
        {

            decimal agv = (decimal)(DutyTotal - dutysum) / (DutyTotal);
            UserChart.Series["查看项目"].Points.AddXY("执勤记录", agv);

        }

        int tempmonth = 1;
        if (int.Parse(dptMonth.SelectedValue) < 12)
        {
            tempmonth = int.Parse(dptMonth.SelectedValue) + 1;
        }

        DateTime tempdt = new DateTime(int.Parse(dptYear.SelectedValue), tempmonth, 1);
        DateTime dt1 = tempdt.AddDays(-1);
        if (Convert.ToInt16(dt1.DayOfWeek) != 0)
        {
            if (Convert.ToInt16(dt1.DayOfWeek) == 5)
            {
                tempdt = tempdt.AddDays(2);
            }
            if (Convert.ToInt16(dt1.DayOfWeek) == 6)
            {
                tempdt = tempdt.AddDays(1);
            }
        }


        DateTime StartTime = new DateTime(year, month, 1);
        if (Convert.ToInt16(StartTime.DayOfWeek) > 5)
        {
            if (Convert.ToInt16(StartTime.DayOfWeek) == 6)
                StartTime = StartTime.AddDays(2);
            if (Convert.ToInt16(StartTime.DayOfWeek) == 0)
                StartTime = StartTime.AddDays(1);
        }
        //车场日登记
        List<ParkLogs> parks = parkdb.ParkLogs.Where(o => o.DepartmentID == info.DepartmentID && o.Date.Value >= StartTime && o.Date.Value <= tempdt).ToList();
        if (parkTotal == 0)
        {
            UserChart.Series["查看项目"].Points.AddXY("车场日登记", DBNull.Value);

        }
        else
        {

            decimal agv = (decimal)(parkTotal - parks.Count()) / (decimal)parkTotal;
            if (agv < 0)
            {
                agv = 0;
            }
            UserChart.Series["查看项目"].Points.AddXY("车场日登记", agv);
        }


        UserChart.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
        UserChart.Series["查看项目"].LabelFormat = "0.00" + "%";

        //车辆装备检查统计

        string where = " departmentId = '" + DepartmentID + "' and year(Daydate)=" + year + " and month(daydate)=" + month + " ";
        int RealCount = VehicleDayItem.GetVichileDayCount(where);
        double percent = (double)(monthDayCount - RealCount) / monthDayCount;

        UserChart.Series["查看项目"].Points.AddXY("车辆装备检查", percent);

    }

    protected void calSchedule_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        dptYear.SelectedValue = e.NewDate.Year.ToString();
        dptMonth.SelectedValue = e.NewDate.Month.ToString();
        lbtnQuery_Click(this, new EventArgs());
        //bindChart();

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string Where = "DepartmentId='" + dptDepartment2.SelectedValue + "' and IsDelete=0 ";
        string url = "../../BasicBase/SoldierArchive/SoldierResult.aspx?RootMenuId=200000&Where=" + Server.HtmlEncode(Where);
        Response.Redirect(url);
    }
}
